/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.f2prateek.rx.preferences3.slice;

import com.f2prateek.rx.preferences3.Preference;
import com.f2prateek.rx.preferences3.ResourceTable;
import com.f2prateek.rx.preferences3.RxSharedPreferences;
import com.jakewharton.rxbinding4.component.RxAbsButton;
import com.jakewharton.rxbinding4.component.RxText;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.openharmony.schedulers.OpenHarmonySchedulers;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Checkbox;
import ohos.agp.components.TextField;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.util.concurrent.TimeUnit;

public class MainAbilitySlice extends AbilitySlice {

    Checkbox foo1Checkbox;
    Checkbox foo2Checkbox;
    TextField foo1EditText;
    TextField foo2EditText;

    Preference<Boolean> fooBoolPreference;
    Preference<String> fooTextPreference;
    CompositeDisposable disposables;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        foo1Checkbox = (Checkbox) findComponentById(ResourceTable.Id_foo_1);
        foo2Checkbox = (Checkbox) findComponentById(ResourceTable.Id_foo_2);
        foo1EditText = (TextField) findComponentById(ResourceTable.Id_text_1);
        foo2EditText = (TextField) findComponentById(ResourceTable.Id_text_2);

        // Preferences
        Preferences preferences = new DatabaseHelper(this).getPreferences("rx_preferences");
        RxSharedPreferences rxPreferences = RxSharedPreferences.create(preferences);

        // foo
        fooBoolPreference = rxPreferences.getBoolean("fooBool");
        fooTextPreference = rxPreferences.getString("fooText");
    }

    @Override
    protected void onActive() {
        super.onActive();
        disposables = new CompositeDisposable();
        bindPreference(foo1Checkbox, fooBoolPreference);
        bindPreference(foo2Checkbox, fooBoolPreference);
        bindPreference(foo1EditText, fooTextPreference);
        bindPreference(foo2EditText, fooTextPreference);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        disposables.dispose();
    }

    void bindPreference(final Checkbox checkBox, Preference<Boolean> preference) {
        // Bind the preference to the checkbox.
        disposables.add(preference.asObservable()
                .observeOn(OpenHarmonySchedulers.mainThread())
                .subscribe(checkBox::setChecked));
        // Bind the checkbox to the preference.
        disposables.add(RxAbsButton.checkedStateChanges(checkBox)
                .skip(1) // First emission is the original state.
                .subscribe(preference.asConsumer()));
    }

    void bindPreference(final TextField editText, Preference<String> preference) {
        disposables.add(preference.asObservable()
                .observeOn(OpenHarmonySchedulers.mainThread())
                .filter(s -> !editText.isFocused())
                .subscribe(editText::setText));
        disposables.add(RxText.textUpdateEvents(editText)
                .skip(1) // First emission is the original state.
                .debounce(500, TimeUnit.MILLISECONDS) // Filter out UI events that are emitted in quick succession.
                .map(e -> e.text())
                .subscribe(preference.asConsumer()));
    }

}
